﻿<?php   
require_once( "classes/tools.class.php" );

if( !Tools::check_ajax_referer() ) {
    header( 'Location: index.php' );
    exit(0);
}

$src = isset($_REQUEST["source"]) ? $_REQUEST["source"] : '';
$oper = isset($_REQUEST["oper"]) ? $_REQUEST["oper"] : '';

if( $src == '' || $oper == '' )
    die("Нарушение формата данных.");
 
if( !($conf = parse_ini_file('config.ini', TRUE)))
    die("Конфигурационный файл не найден");

$dsn = "mysql:host=".$conf['DB']['dbserv'].";".
    "dbname=".$conf['DB']['dbname'].";".
    "charset=".( isset($conf['DB']['charset']) ? $conf['DB']['charset'] : "utf8" ).";";

try {
    $db = new PDO( $dsn, $conf['DB']['dbuser'], $conf['DB']['dbpass'] );
	$db->exec('SET CHARACTER SET utf8');	
} 
catch (PDOException $e) {
    die();
}

switch ($src)
{
    case "ColorDictionary":
        require_once( "classes/colorDic.class.php" );
	    $ob = new ColorDictionary($db);
        break;
        
    case "VendorDictionary":
        require_once( "classes/vendorDic.class.php" );
		$ob = new VendorDictionary($db);
        break;
        
    case "ModelDictionary":
        require_once( "classes/modelDic.class.php" );
        $ob = new ModelDictionary($db);
        break;
        
    case "CarCatalogue":
        require_once( "classes/cars.class.php" );
        $ob = new Cars($db);
        break;

    case "CarPrice":
        require_once( "classes/price.class.php" );
        $ob = new Price($db);
        break;
        
    case "CarTree":
        if( $_REQUEST["node_id"] === '0' ) {
            require_once( "classes/vendorDic.class.php" );
            $ob = new VendorDictionary($db);            
        }
        else {
            require_once( "classes/modelDic.class.php" );
            $ob = new ModelDictionary($db);            
        }
        break;
        
    default:
        die("Нарушение формата данных.");
}

switch ($oper)
{
    case "add":  $ob->Insert();  break;
    case "edit": $ob->Update();  break;
    case "del":  $ob->Delete();  break;
    case "find": $ob->Find();    break;
    case "tree": $ob->GetList(); break;
    case "page": $ob->GetPage(); break;
}

?>
